﻿// -----------------------------------------------------------------------
// <copyright file="SwitchDepartment_DAL.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace PMSGSC.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    /// 
    
    public class Maker_DAL: BaseDAL
    {
        // Lấy tất cả maker
        public static List<Maker> GetAll(int shipID)
        {
            DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.Makers);
            return DB.Makers.Where(n=>n.Ship_ID == shipID).ToList();
        }
        // Lấy Maker theo Id
        public static Maker GetDataById(int id)
        {
            return DB.Makers.Where(m => m.Maker_ID == id).FirstOrDefault();
        }
        // Lấy Maker theo Id
        public static Maker GetDataByCode(string code, int shipID)
        {
            return DB.Makers.Where(m => (m.CodeMaker == code) && (m.Ship_ID == shipID)).FirstOrDefault();
        }
        // Lấy Maker theo Id
        public static Maker GetDataByName(string name, int shipID)
        {
            return DB.Makers.Where(m => (m.NameMaker == name) && (m.Ship_ID == shipID)).FirstOrDefault();
        }
        //Thêm một Maker
        public static bool Insert(Maker maker)
        {
            try
            {
                DB.Makers.InsertOnSubmit(maker);
                DB.SubmitChanges();
                return true;
            }
            catch {
                return false;
            }
           
        }
        // Xóa một maker
        public static bool Delete(Maker maker)
        {
            try
            {
                DB.Makers.DeleteOnSubmit(maker);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
           
        }
        // Cập nhật maker
        public static bool Update(Maker maker)
        {
            try
            {
                //Maker objMaker = GetDataById(maker.Maker_ID);
                //objMaker.CodeMaker = maker.CodeMaker;
                //objMaker.NameMaker = maker.NameMaker;
                //objMaker.Address = maker.Address;
                //objMaker.Tell = maker.Tell;
                //objMaker.Email = maker.Email;
                //objMaker.ContactPerson = maker.ContactPerson;
                //objMaker.Fax = maker.Fax;
                //objMaker.Note = maker.Note;
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
       
        // Kiểm tra xem Maker Code có tồn tại chưa ?
        public static bool IsExits(Maker maker)
        {
            Maker objMaker = null;
            objMaker = DB.Makers.Where(m => (m.CodeMaker == maker.CodeMaker) &&
                (m.Maker_ID != maker.Maker_ID) && (m.Ship_ID == maker.Ship_ID)).FirstOrDefault();
            return objMaker != null ? true : false;
        }
        public static bool IsRelationship(Maker maker)
        {
            Equipment equipment = DB.Equipments.Where(n => n.Maker_ID == maker.Maker_ID).FirstOrDefault();
            if (null != equipment)
                return false;

            PartsInEquipment part = DB.PartsInEquipments.Where(n => n.Maker_ID == maker.Maker_ID).FirstOrDefault();
            if (null != part)
                return false;

            return true;
        }       

    }
}
